package test;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class IndexSynoymMapDemo {

	public static void main(String[] args) throws IOException, Exception {
		String index = "D:\\index";
		IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));
		IndexSearcher searcher = new IndexSearcher(reader);
		ScoreDoc[] hits = null;
		String queryString = "欢呼";
		Query query = null;
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
		
		try {
			QueryParser qp = new QueryParser(Version.LUCENE_43, "body", analyzer);
			query = qp.parse(queryString);
		} catch (org.apache.lucene.queryparser.classic.ParseException e) {			
		}
		
		if(searcher != null){
			TopDocs results = searcher.search(query, 10);
			hits = results.scoreDocs;
			if(hits.length > 0){
				System.out.println("找到："+hits.length+" 个结果！");
			}
			for(ScoreDoc s:hits){
				Document doc = searcher.doc(s.doc);
				System.out.println(s.toString()+" 文件名："+doc.get("path")+" "+doc.getFields()+" ");
				System.out.println(doc.get("body"));
			}
		}
	}
		
}

	
	
	
	
	
	
